package com.k.util.excel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.Objects;
import java.util.stream.IntStream;

public class POIUtil {
    public static final int PERCENT_WIDTH = 50;
    public static final int PERCENT_HEIGHT = 20;
    public static final float PXTOPT = 0.75f;

    public static void createContent(Workbook workbook, Sheet sheet, Row row, Cell cell, String content,
                                     CellStyle cellStyle) {
        cell.setCellValue(content);
        if (Objects.nonNull(cellStyle)) {
            cell.setCellStyle(cellStyle);
        }
    }

    public static void createContent(Workbook workbook, Sheet sheet, Row row, int columnIndex, String content,
                                     CellStyle cellStyle) {
        Cell cell = row.getCell(columnIndex) == null ? row.createCell(columnIndex) : row.getCell(columnIndex);
        createContent(workbook, sheet, row, cell, content, cellStyle);
    }

    public static void createContent(Workbook workbook, Sheet sheet, int rowIndex, int columnIndex, String content,
                                     CellStyle cellStyle) {
        Row row = sheet.getRow(rowIndex) == null ? sheet.createRow(rowIndex) : sheet.getRow(rowIndex);
        createContent(workbook, sheet, row, columnIndex, content, cellStyle);
    }
    public static void createContent(Workbook workbook, Sheet sheet, int rowIndex, int columnIndex, String content) {
        Row row = sheet.getRow(rowIndex) == null ? sheet.createRow(rowIndex) : sheet.getRow(rowIndex);
        createContent(workbook, sheet, row, columnIndex, content, null);
    }

    public static String getKong(int c) {
        StringBuilder sb = new StringBuilder();
        IntStream.range(0,c).forEach(a->{sb.append(" ");});
        return sb.toString();
    }

    public static void merge(Sheet sheet, int firstRow, int lastRow, int firstCol, int lastCol) {
        sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol));
    }

    public static CellStyle getTitleStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        return style;
    }
    public static CellStyle getLineCellStyle(Workbook workbook) {
        CellStyle style = workbook.createCellStyle();
        style.setVerticalAlignment(VerticalAlignment.TOP);
        Font font = workbook.createFont();
        font.setBold(true);
        style.setFont(font);
        style.setWrapText(true);
        return style;
    }
}
